// pages/product/product.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loading: true,
    isErr: false,
    detail: null,
    show: false,
    cartCount: wx.getStorageSync('cartCount') || '',
    productSkuData: null,
    imgs: [{
        "img_url": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/6611413789ba4d85df9d0435d6193d4e.jpg?f=webp",
        "img_url_webp": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/6611413789ba4d85df9d0435d6193d4e.jpg?f=webp",
        "type": "img"
      },
      {
        "img_url": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/39d7ecbcfae59cc7fc8f5031733b0d23.jpg?f=webp",
        "img_url_webp": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/39d7ecbcfae59cc7fc8f5031733b0d23.jpg?f=webp",
        "type": "img"
      },
      {
        "img_url": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/90ddb730b5d799d37d1e19c59cecd2e3.jpg?f=webp",
        "img_url_webp": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/90ddb730b5d799d37d1e19c59cecd2e3.jpg?f=webp",
        "type": "img"
      },
      {
        "img_url": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/3415fc926ddd5b0499e9b3c396c014dc.jpg?f=webp",
        "img_url_webp": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/3415fc926ddd5b0499e9b3c396c014dc.jpg?f=webp",
        "type": "img"
      },
      {
        "img_url": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/10b96ae302a33edd5914e3ef83b5125e.jpg?f=webp",
        "img_url_webp": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/10b96ae302a33edd5914e3ef83b5125e.jpg?f=webp",
        "type": "img"
      },
      {
        "img_url": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/943573e1fd512a28bcfa91be32470b78.jpg?f=webp",
        "img_url_webp": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/943573e1fd512a28bcfa91be32470b78.jpg?f=webp",
        "type": "img"
      },
      {
        "img_url": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/65e3fd95623e49d70fd61f5540117241.jpg?f=webp",
        "img_url_webp": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/65e3fd95623e49d70fd61f5540117241.jpg?f=webp",
        "type": "img"
      },
      {
        "img_url": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/1cddb3c23735c7078e2e38e9087fb353.jpg?f=webp",
        "img_url_webp": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/1cddb3c23735c7078e2e38e9087fb353.jpg?f=webp",
        "type": "img"
      },
      {
        "img_url": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/c9d7056cf2a4b51e63a8e9528ecba644.jpg?f=webp",
        "img_url_webp": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/c9d7056cf2a4b51e63a8e9528ecba644.jpg?f=webp",
        "type": "img"
      },
      {
        "img_url": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/7e26a37b269fdfaf48fa429976c5bd57.jpg?f=webp",
        "img_url_webp": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/7e26a37b269fdfaf48fa429976c5bd57.jpg?f=webp",
        "type": "img"
      },
      {
        "img_url": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/d693cf3349d82a03895a5cda5c686cda.jpg?f=webp",
        "img_url_webp": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/d693cf3349d82a03895a5cda5c686cda.jpg?f=webp",
        "type": "img"
      },
      {
        "img_url": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/fb728beee3f1ad0a4aa8eb8df69dcf9e.jpg?f=webp",
        "img_url_webp": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/fb728beee3f1ad0a4aa8eb8df69dcf9e.jpg?f=webp",
        "type": "img"
      },
      {
        "img_url": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/01b6d19f24fd9d6dba1aa5c7467dde46.jpg?f=webp",
        "img_url_webp": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/01b6d19f24fd9d6dba1aa5c7467dde46.jpg?f=webp",
        "type": "img"
      },
      {
        "img_url": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/5e66f143b06c014cb5503ed9fbb1f946.jpg?f=webp",
        "img_url_webp": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/5e66f143b06c014cb5503ed9fbb1f946.jpg?f=webp",
        "type": "img"
      },
      {
        "img_url": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/e6e55132605ab1816fd445758817868b.jpg?f=webp",
        "img_url_webp": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/e6e55132605ab1816fd445758817868b.jpg?f=webp",
        "type": "img"
      },
      {
        "img_url": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/aff335a83486f3bf563b215a372fca93.jpg?f=webp",
        "img_url_webp": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/aff335a83486f3bf563b215a372fca93.jpg?f=webp",
        "type": "img"
      },
      {
        "img_url": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/13b88bb48c0b6aa9cc3dd387ca52fba2.jpg?f=webp",
        "img_url_webp": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/13b88bb48c0b6aa9cc3dd387ca52fba2.jpg?f=webp",
        "type": "img"
      },
      {
        "img_url": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/2cd6819bbdf3f58c9236b04227657890.jpg?f=webp",
        "img_url_webp": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/2cd6819bbdf3f58c9236b04227657890.jpg?f=webp",
        "type": "img"
      },
      {
        "img_url": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/dc685c11d0a7ef04d38657182a42f3f0.jpg?f=webp",
        "img_url_webp": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/dc685c11d0a7ef04d38657182a42f3f0.jpg?f=webp",
        "type": "img"
      },
      {
        "img_url": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/f86866fc064bb8e0d08d36127a44810f.jpg?f=webp",
        "img_url_webp": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/f86866fc064bb8e0d08d36127a44810f.jpg?f=webp",
        "type": "img"
      },
      {
        "img_url": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/ab3f4b59a1b4771f1c7b01b40daf8334.jpg?f=webp",
        "img_url_webp": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/ab3f4b59a1b4771f1c7b01b40daf8334.jpg?f=webp",
        "type": "img"
      },
      {
        "img_url": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/1f3bec1c0b1c880c8d337e34ca96d68a.jpg?f=webp",
        "img_url_webp": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/1f3bec1c0b1c880c8d337e34ca96d68a.jpg?f=webp",
        "type": "img"
      },
      {
        "img_url": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/9c6309a5ca3fc08af85601d057f3094b.jpg?f=webp",
        "img_url_webp": "//cdn.cnbj1.fds.api.mi-img.com/mi-mall/9c6309a5ca3fc08af85601d057f3094b.jpg?f=webp",
        "type": "img"
      }
    ]
  },

  getDetail(id) {
    // 获取商品详情
    wx.$http.get('product/get/' + id).then(res => {
      console.log(res)
      this.setData({
        detail: res.data,
        loading: false
      })
    }).catch(err => {
      console.error(err)
      this.setData({
        loading: false,
        isErr: true
      })
    })

    // 获取商品规格
    wx.$http.get('product/getProductSkuList/' + id).then(res => {
      console.log(res)
      this.setData({
        productSkuData: res.data
      })
    })

  },

  openSku() {
    this.setData({
      show: true
    })
  },

  goCart() {
    wx.switchTab({
      url: '/pages/cart/cart',
    })
  },

  confirmSku(result) {
    //  将商品加入购物车
    wx.$http.post('product/editUserSku', result.detail).then(res => {
      // 获取最新的购物车
      wx.$http.get('product/getUserSku').then(res => {
        // 隐藏商品sku弹框,设置购物车数量
        this.setData({
          show: false,
          cartCount: res.data.reduce((prev, cur) => {
            return prev + cur.num
          }, 0) || ''
        })
        wx.setStorageSync('cartCount', res.data.reduce((prev, cur) => {
          return prev + cur.num
        }, 0))
      })
    })

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.getDetail(options.id)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      cartCount: wx.getStorageSync('cartCount') || ''
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})